package com.sec.android.app.myfiles.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.feature.KnoxMgr;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.util.UiUtils;

public class AddShortcutDialogFragment extends AbsDialogFragment {
    private AlertDialog mAlertDialog = null;
    private ChooseCallback mCallback;

    public static AddShortcutDialogFragment getInstance(ChooseCallback callback) {
        AddShortcutDialogFragment addShortcutDialog = new AddShortcutDialogFragment();
        addShortcutDialog.setCallback(callback);
        return addShortcutDialog;
    }

    private void setCallback(ChooseCallback callback) {
        mCallback = callback;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        Log.d(this, "onSaveInstanceState()");

        DialogFragmentUtils.setTargetFragment(this);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected Dialog _createDialog() {
        Log.d(this, "onCreateDialog()");

        View v = LayoutInflater.from(getActivity()).inflate(R.layout.add_shortcut_dialog, null);
        initView(v);

        AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
        dialog.setTitle(R.string.add_shortcut_to);
        dialog.setView(v);

        dialog.setNegativeButton(R.string.cancel, null);
        mAlertDialog = dialog.create();

        return mAlertDialog;
    }

    private void initView(View v) {
        TextView addShortcutToMyFiles = (TextView) v.findViewById(R.id.add_shortcut_to_myfile_button);
        TextView addShortcutToHome = (TextView) v.findViewById(R.id.add_shortcut_to_home_button);

        addShortcutToMyFiles.setOnClickListener(mToMyFileClickListener);
        addShortcutToHome.setOnClickListener(mToHomeScreenClickListener);

        if (!supportShortcutOnHomeScreen()) {
            addShortcutToHome.setEnabled(false);
            addShortcutToHome.setAlpha(0.4f);
        }
    }

    public OnClickListener mToMyFileClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            onItemClick((TextView) v, FileRecord.ShortcutType.MyFiles);
        }
    };

    public OnClickListener mToHomeScreenClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            onItemClick((TextView) v, FileRecord.ShortcutType.HomeScreen);
        }
    };

    public void onItemClick(TextView view, FileRecord.ShortcutType type) {
        mCallback.onClick(type);
        mAlertDialog.dismiss();
    }

    public interface ChooseCallback {
        void onClick(FileRecord.ShortcutType type);
    }

    private boolean supportShortcutOnHomeScreen() {
        if (UiUtils.isUPSM(mContext) || UiUtils.isEasyMode(mContext) || AppFeatures.isBYOD() || (AppFeatures.isKnox() && KnoxMgr.getInstance(mContext).isLightWeightContainer())) {
            return false;
        }
        return true;
    }

}
